package com.stu.serive.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.stu.Dto.JobApplicationDTO;
import com.stu.Dto.JobInformationDTO;
import com.stu.Dto.JobPageQueryDTO;
import com.stu.Dto.JobWorkingDTO;
import com.stu.constant.StatusConstant;
import com.stu.context.BaseContext;
import com.stu.mapper.JobInformationMapper;
import com.stu.pojo.JobApplication;
import com.stu.pojo.JobWorking;
import com.stu.pojo.JobInformation;
import com.stu.result.PageResult;
import com.stu.serive.JobInformationService;
import com.stu.vo.JobApplicationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class JobInformationServiceImpl implements JobInformationService {
    @Autowired
    private JobInformationMapper jobInformationMapper;

    /**
     * 查询工作信息
     *
     * @param jobPageQueryDTO
     * @return
     */
    public PageResult pageQuery(JobPageQueryDTO jobPageQueryDTO) {
        //使用jobPageQueryDTO作为参数，执行一个分页查询，并将查询结果（一个Page对象，其中包含了DishVO类型的元素）存储在page变量中。
        PageHelper.startPage(jobPageQueryDTO.getPage(), jobPageQueryDTO.getPageSize());
        Page<JobInformation> page = jobInformationMapper.pageQuery(jobPageQueryDTO);
        //修改审核状态
        List<JobInformation> jobList = page.getResult();
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        for (JobInformation jobInfo : jobList) {
            //获取工作的id
            Integer jobId = jobInfo.getId();
            //查询是否申请工作
            Integer id = jobInformationMapper.selectIdByCurrentId(currentId, jobId);
            if (id != null) {
                jobInfo.setApplicationStatus("已申请");
            }
        }
        log.info("jobList:{}", jobList);
        return new PageResult(page.getTotal(), jobList);
    }

    /**
     * 新增工作申请信息
     *
     * @param dto
     */
    @Transactional
    public void insertJobApplication(JobApplicationDTO dto) {
        //获取学生id
        Long currentId = BaseContext.getCurrentId();
        //设置stu_id
        dto.setStuId(Math.toIntExact(currentId));
        //修改时间
        dto.setApplicationTime(LocalDateTime.now());
        //对jobApplication表进行添加操作
        jobInformationMapper.insertJobApplication(dto);
        //对job信息的申请人数进行修改
        jobInformationMapper.updateJobInformation(dto.getJobId());

    }

    /**
     * 学工部
     * 批量确认学生申请，将该学生加工作入职表
     *
     * @param dto
     */
    public void insertJobWorking(JobWorkingDTO dto) {

        //获取员工id
        Long currentId = BaseContext.getCurrentId();
        //创建entity对象
        JobWorking jobWorking = new JobWorking();
        //转换成entity对象
        BeanUtils.copyProperties(dto, jobWorking);

        //修改认证员工
        jobWorking.setConfirmAdmin(Math.toIntExact(currentId));
        //修改入职时间为确认时间
        jobWorking.setWorkTime(LocalDateTime.now());

        log.info("jobWorking:{}", jobWorking);

        //对jobApplication表进行添加操作
        jobInformationMapper.insertJobWorking(jobWorking);
        //对job信息的申请人数进行修改
        jobInformationMapper.updateJobWorking(dto.getJobId());
    }

    /**
     * 学工部
     * 批量添加工作信息
     *
     * @return
     */
    public void insertJobInformation(JobInformationDTO jobInformationDTO) {

        //log.info("批量添加工作信息jobInformationDTO:{}",jobInformationDTO);
        JobInformation jobInformation = new JobInformation();
        BeanUtils.copyProperties(jobInformationDTO, jobInformation);
        //log.info("批量添加工作信息jobInformation:{}",jobInformation);

        jobInformationMapper.insertJobInformation(jobInformation);
    }

    /**
     * 学工部
     * 修改工作信息
     *
     * @param jobInformationDTO
     */
    public void updateJobInformation(JobInformationDTO jobInformationDTO) {
        jobInformationMapper.updateAllJobInformation(jobInformationDTO);
    }

    /**
     * 学工部
     * 删除工作信息
     *
     * @param ids
     */
    @Transactional
    public void deleteJobInformation(List<Integer> ids) {
        //删除工作信息表
        jobInformationMapper.deleteJobInformation(ids);
        //删除相关的申请表
        jobInformationMapper.deleteJobApplication(ids);
        //删除相关的入职表
        jobInformationMapper.deleteJobWorking(ids);
    }

    /**
     * 查询已经申请的工作
     *
     * @return
     */
    public List<JobApplicationVo> selectApplication() {
        List<JobApplicationVo> jobApplicationVoList = new ArrayList<>();
        Long currentId = BaseContext.getCurrentId();
        List<JobApplication> jobApplicationList = jobInformationMapper.selectApplicationById(currentId);

        for (JobApplication jobApplication : jobApplicationList) {
            if (jobApplication == null) {
                continue; // 跳过空的 JobApplication
            }

            JobApplicationVo jobApplicationVo = new JobApplicationVo();
            BeanUtils.copyProperties(jobApplication, jobApplicationVo);

            Long jobId = Long.valueOf(jobApplication.getJobId());
            if (jobId != null) {
                JobInformation jobInformation = jobInformationMapper.selectInformation(Math.toIntExact(jobId));
                if (jobInformation != null) {
                    BeanUtils.copyProperties(jobInformation, jobApplicationVo);
                }
            }

            // 处理审批状态
            String applicationStatus = jobApplicationVo.getApplicationStatus();
            if (Objects.equals(applicationStatus, "1")) {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_1);
            } else if (Objects.equals(applicationStatus, "0")) {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_0);
            } else {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_2);
            }

            jobApplicationVoList.add(jobApplicationVo);
        }

        return jobApplicationVoList;
    }

    /**
     * 查询所有勤工俭学申请
     *
     * @return
     */
    public List<JobApplicationVo> selectApplicationAll() {
        //返回的数组
        List<JobApplicationVo> jobApplicationVoList = new ArrayList<>();
        //临时的返回量


        //根据当前学生的id查询申请表，获取该学生申请的工作
        List<JobApplication> jobApplicationList = jobInformationMapper.selectApplicationAll();
        for (JobApplication jobApplication : jobApplicationList) {
            JobApplicationVo jobApplicationVo = new JobApplicationVo();
            //根据获取工作信息
            JobInformation jobInformation = jobInformationMapper.selectInformation(jobApplication.getJobId());

            //操作Vo对象
            BeanUtils.copyProperties(jobApplication, jobApplicationVo);
            BeanUtils.copyProperties(jobInformation, jobApplicationVo);
            log.info("jobApplicationVo:{}", jobApplicationVo);

            //判断审批状态
            if (Objects.equals(jobApplicationVo.getApplicationStatus(), "1")) {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_1);
            } else if (Objects.equals(jobApplicationVo.getApplicationStatus(), "0")) {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_0);
            } else {
                jobApplicationVo.setApplicationStatus(StatusConstant.STATUS_2);
            }

            //添加进数组中
            jobApplicationVoList.add(jobApplicationVo);
        }
        log.info("jobApplicationVoList:{}", jobApplicationVoList);
        return jobApplicationVoList;
    }
}
